// Package first
// @Author: zhangdi
// @File: first_dic
// @Version: 1.0.0
// @Date: 2023/11/20 14:32
package first

import (
	"server/application/db/system/columns"
	"server/application/db/system/dbdto"
	"server/application/db/system/dbsvc"
	"server/pkg/mylog"
	"server/pkg/where"
)

// SetFirstDicValue 字典的初始值
func SetFirstDicValue() {
	dicList := formatDicFirstData()
	for _, dicInfo := range dicList {
		wheres := where.Format(where.Opt(columns.SysDictionaryDicKey, "=", dicInfo.DicKey))
		info, _ := dbsvc.SysDictionaryFindOne(wheres)
		if info != nil && info.ID > 0 { //已有数据则进行修改(部分字段)
			err := dbsvc.SysDictionaryUpdate(wheres, &dbdto.SysDictionaryUpdate{
				//只更改类型字段
				DicValueType:  dicInfo.DicValueType,
				ValueDataType: dicInfo.ValueDataType,
			})
			if err != nil {
				mylog.Error("SetFirstDicValue()-update" + err.Error())
				continue
			}
		} else { //没有数据则进行插入
			_, err := dbsvc.SysDictionaryCreate(&dicInfo)
			if err != nil {
				mylog.Error("SetFirstDicValue()-create" + err.Error())
				continue
			}
		}
	}

}

// 检测并设置初始值
// val 默认值
func formatDicFirstData() []dbdto.SysDictionaryCreate {
	// SysDictionaryCreate slice
	var dictionaries = []dbdto.SysDictionaryCreate{
		{DicValueType: "options", ValueDataType: "string", DicKey: "test", DicValue: "[{\"label\":\"ss\",\"value\":\"ddd\"},{\"label\":\"ff\",\"value\":\"ggg\"}]", Description: "", IsActive: true, DicSort: 10},
		{DicValueType: "entity", ValueDataType: "string", DicKey: "ent", DicValue: "{\"father_table_name\":\"role\",\"son_table_name\":\"user\",\"son_relation_field\":\"role_ids\"}", Description: "", IsActive: true, DicSort: 10},
		{DicValueType: "sql_select", ValueDataType: "string", DicKey: "sql", DicValue: "select username as label,id as value  from sys_users where  1", Description: "sql demo", IsActive: true, DicSort: 10},
		{DicValueType: "options", ValueDataType: "number", DicKey: "user_status", DicValue: "[{\"label\":\"正常\",\"value\":\"1\"},{\"label\":\"禁用\",\"value\":\"0\"}]", Description: "用户状态", IsActive: true, DicSort: 10},
		{DicValueType: "sql_select", ValueDataType: "number", DicKey: "stations", DicValue: "select id as value,station_name as label  from pub_gas_stations", Description: "气站列表", IsActive: true, DicSort: 10},
		{DicValueType: "options", ValueDataType: "string", DicKey: "BottleModels", DicValue: "[{\"label\":\"YSP12/4.9\",\"value\":\"YSP12/4.9\"},{\"label\":\"YSP23.9/10\",\"value\":\"YSP23.9/10\"},{\"label\":\"YSP29.8/12.4\",\"value\":\"YSP29.8/12.4\"},{\"label\":\"YSP35.5/14.8\",\"value\":\"YSP35.5/14.8\"},{\"label\":\"YSP118/49.5\",\"value\":\"YSP118/49.5\"},{\"label\":\"YSP118/液/49.5\",\"value\":\"YSP118/液/49.5\"},{\"label\":\"YSP35.5/壹/14.8\",\"value\":\"YSP35.5/壹/14.8\"},{\"label\":\"YSP35.5/贰/14.8\",\"value\":\"YSP35.5/贰/14.8\"}]", Description: "产品型号", IsActive: true, DicSort: 10},
		{DicValueType: "options", ValueDataType: "number", DicKey: "OutYears", DicValue: "[{\"label\":\"2022\",\"value\":\"2022\"},{\"label\":\"2023\",\"value\":\"2023\"}]", Description: "出厂年份", IsActive: true, DicSort: 10},
		{DicValueType: "options", ValueDataType: "number", DicKey: "Months", DicValue: "[{\"label\":\"1\",\"value\":\"1\"},{\"label\":\"2\",\"value\":\"2\"},{\"label\":\"3\",\"value\":\"3\"},{\"label\":\"4\",\"value\":\"4\"},{\"label\":\"5\",\"value\":\"5\"},{\"label\":\"6\",\"value\":\"6\"},{\"label\":\"7\",\"value\":\"7\"},{\"label\":\"8\",\"value\":\"8\"},{\"label\":\"9\",\"value\":\"9\"},{\"label\":\"10\",\"value\":\"10\"},{\"label\":\"11\",\"value\":\"11\"},{\"label\":\"12\",\"value\":\"12\"}]", Description: "月份", IsActive: true, DicSort: 10},
		{DicValueType: "options", ValueDataType: "number", DicKey: "sex", DicValue: "[{\"label\":\"男\",\"value\":\"1\"},{\"label\":\"女\",\"value\":\"0\"},{\"label\":\"未知\",\"value\":\"2\"}]", Description: "性别", IsActive: true, DicSort: 10},
		{DicValueType: "sql_search", ValueDataType: "string", DicKey: "search_lot", DicValue: " select lot_no as value,lot_no as label from pub_lots where lot_no  LIKE '%{search_text}%' LIMIT 20", Description: "远程搜索批次", IsActive: true, DicSort: 10},
		{DicValueType: "sql_search", ValueDataType: "string", DicKey: "search_jiaofu_by_batchno", DicValue: " select id as value,out_lot_ids as label from pub_delivery_records where out_lot_ids LIKE '%{search_text}%' LIMIT 20", Description: "根据批次号远程搜索交付说明", IsActive: true, DicSort: 10},
	}

	return dictionaries
}
